package com.wct.util;

import java.nio.ByteBuffer;

import org.bson.BSONObject;

import com.wct.db.DbUtils;
import com.wct.define.Constant;
import com.wct.struct.DeviceGps;
import com.wct.struct.GPSPacket;
import com.wct.struct.SmsPacket;
import com.wct.struct.SyscfgPacket;

public class PackageBuilder {
	private DbUtils dbUtils;
	private double step = 0.0001;
	private double range = 0.1d;
	private int direct = -1;
	private short LonDegreebase = (short)104;
	private short LonCentbase = (short)30;
	private short Lonsecondbase = (short)30;
	private Double center = GPSUtil.convertToDouble(LonDegreebase, LonCentbase, Lonsecondbase);
	private Double current = center.doubleValue();
	private int size = new DeviceGps().size();
	private ByteBuffer MakeSyscfgFromDevice(BSONObject info, short cmd, short cmdtype){
		SyscfgPacket packet = new SyscfgPacket();
		packet.DataLen.set((short) packet.getByteBuffer().remaining());
		packet.StartId.set(Constant.PACKAGESTART);
		packet.Cmd.set(cmd);
		packet.CmdType.set(cmdtype);
		Boolean power = (Boolean)info.get(Constant.Device.powerStatus);
		if (power!= null && !power) {
			packet.Data.bDevice.set((short) 0x00);					
		}
		else{
			packet.Data.bDevice.set((short) 0x01);
		}
		Boolean music = (Boolean)info.get(Constant.Device.voiceStatus);
		if (music!=null && !music) {
			packet.Data.bMusic.set((short) 0x00);
		}
		else{
			packet.Data.bMusic.set((short) 0x01);
		}
		
		packet.Data.CtrlNumber.set((String)info.get(Constant.Device.mainNumber));
		packet.Data.UrgentNumber.set((String)info.get(Constant.Device.backupNumber1));
		packet.Data.UrgentNumber2.set((String)info.get(Constant.Device.backupNumber2));
		packet.Data.UrgentNumber3.set((String)info.get(Constant.Device.backupNumber3));
		Integer interval = (Integer)info.get("interval");
		if (interval != null) {
			packet.Data.InterVal.set(interval.shortValue() );
		}
		else {
			packet.Data.InterVal.set((short) 0x00);
		}
		packet.Data.Mode.set(((Integer)info.get(Constant.Device.mode)).shortValue());
		packet.EndId.set(Constant.PACKAGEEND);
		
		return packet.getByteBuffer();
	}
	
	public ByteBuffer MakeSyscfgFromDevice(String deviceid, short cmd, short cmdtype){
		BSONObject info = dbUtils.getDevice(deviceid);
		if (deviceid != null) {
	        return MakeSyscfgFromDevice(info, cmd, cmdtype);
        }
		else {
			return null;
		}
	}
	public ByteBuffer MakeDefaultSyscfgFromDevice(String deviceid){
		BSONObject info = dbUtils.getDevice(deviceid);
		if (deviceid != null) {
	        return MakeSyscfgFromDevice(info, (short)1, (short)1);
        }
		else {
			return null;
		}
	}
	
	public DbUtils getDbUtils() {
		return dbUtils;
	}

	public void setDbUtils(DbUtils dbUtils) {
		this.dbUtils = dbUtils;
	}
	
	public  ByteBuffer makeSmsPacket(String deviceid){
		String data = "test";
		SmsPacket packet = new SmsPacket(data.length());
		packet.DataLen.set((short) packet.getByteBuffer().remaining());
		packet.StartId.set(Constant.PACKAGESTART);
		packet.Cmd.set((short) 5);
		packet.CmdType.set((short) 2);
		byte[] array = data.getBytes();
		for(int i = 0;i<data.length();i++) {
	        packet.Data[i].set(array[i]);
        }
		packet.EndId.set(Constant.PACKAGEEND);
		return packet.getByteBuffer();
	}
	
	public ByteBuffer makelogin(long deviceid){
		GPSPacket packet = new GPSPacket();
		packet.StartId.set(Constant.PACKAGESTART);
		packet.CmdType.set((short) 0);
		packet.Cmd.set((short) 0);
		packet.DataLen.set((short)(size+5));
		packet.DeviceSN.set(deviceid);
		packet.Data.Batterry.set(100);
	
		
		packet.EndId.set(Constant.PACKAGEEND);
		
		return packet.getByteBuffer();
	}
	
	public ByteBuffer makeGPS(long deviceid){
		GPSPacket packet = new GPSPacket();
		packet.StartId.set(Constant.PACKAGESTART);
		packet.CmdType.set((short) 1);
		packet.Cmd.set((short)1);
		packet.DataLen.set((short)(size+5));
		packet.DeviceSN.set(deviceid);
		packet.Data.Batterry.set(100);
		packet.Data.LatDegree.set((short) 30);
		packet.Data.LatCent.set((short) 33);
		packet.Data.LatSecond.set((short) 10);
		packet.Data.LonDegree.set((short) 104);
			
		if (direct > 0) {
				current = current + step;
         }
		else {
			current = current - step;
		}
		if (Math.abs(current-center) > range) {
	        direct = direct * -1;
	        
        }
		short array[] = GPSUtil.convertback(current);
		packet.Data.LonDegree.set(array[0]);
		packet.Data.LonCent.set(array[1]);
		packet.Data.LonSecond.set(array[2]);
        
		
		packet.EndId.set(Constant.PACKAGEEND);
		
		return packet.getByteBuffer();
	}
}
